package com.orphan.marketFront.controller;


import com.orphan.marketFront.po.ReturnsPo;
import com.orphan.marketFront.service.IOrderService;
import com.orphan.marketFront.util.RedisCacheUtil;
import com.orphan.marketFront.util.ResultCode;
import com.orphan.marketFront.util.ResultVO;
import com.orphan.marketFront.vo.*;

import com.orphan.util.dto.ReturnsDetailsDto;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController

public class OrderController {
    @Autowired
    RedisCacheUtil redisCacheUtil;
    @Autowired
    IOrderService orderService;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @GetMapping("list")
    public ResultVO list(long userId){
        List<OrderVo> orderVos = orderService.listAll(userId);
        ResultVO resultVO = null;
        if(orderVos.size()!=0){
            resultVO = ResultVO.success(orderVos);
        }else {
            resultVO = ResultVO.fail(ResultCode.ORDER_QUERY_ERROR);
        }
        return resultVO;
    }
    @GetMapping("query")
    public ResultVO query(String orderTime){
        List<OrderVo> orderVos = orderService.queryByTime(orderTime);
        ResultVO resultVO = null;
        if(orderVos.size()!=0){
            resultVO = ResultVO.success(orderVos);
        }else {
            resultVO = ResultVO.fail(ResultCode.ORDER_QUERY_ERROR);
        }
        return resultVO;
    }
    @GetMapping("remove")
    public ResultVO remove(String orderId){
        int remove = orderService.remove(orderId);
        ResultVO resultVO = null;
        if(remove>0){
            resultVO = ResultVO.success(remove);
        }else {
            resultVO = ResultVO.fail(ResultCode.ORDER_REMOVE_ERROR);
        }
        return resultVO;
    }
    @GetMapping("orderDetail/{orderNo}")
    public ResultVO orderDetail(@PathVariable("orderNo") String orderNo){
        List<OrderDetailVo> orderDetailVos = orderService.orderDetailList(orderNo);
        System.out.println("orderDetailVos = =============" + orderDetailVos);
        ResultVO resultVO = null;
        if(orderDetailVos.size()!=0){
            resultVO = ResultVO.success(orderDetailVos);
        }else {
            resultVO = ResultVO.fail(ResultCode.ORDER_DETAIL_ERROR);
        }
        return resultVO;
    }
    @GetMapping("orderReturns")
    public ResultVO orderReturns( String orderId,String goodsName,String currentTime) throws ParseException {
        OrderReturnsVo orderReturnsVos = orderService.orderReturns(orderId, goodsName);
        ResultVO resultVO = null;
        Date s00 =  orderReturnsVos.getOrderTime();;  //获取需要比较的目标时间
        Calendar calendar = Calendar.getInstance(); // 得到日历
        SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

        Date time = sdf.parse(currentTime);
        calendar.setTime(time);// 把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -7); // 设置为7天前
        Date before7days = calendar.getTime(); // 得到7天前的时间
        if (before7days.getTime() > s00.getTime()) {
            // 满足大于7天的用户不再退货
            resultVO = ResultVO.fail(ResultCode.RETURNS_CHECK_FAILED);
        }
        if (before7days.getTime() < s00.getTime()) {
            // 在7天内的订单予以退货
            resultVO = ResultVO.success(ResultCode.RETURNS_CHECK_SUCCESS);
            String returnsNo = orderReturnsVos.getGoodsName()+time;
            orderService.addOrderReturns(orderReturnsVos.getOrderId(),returnsNo,time);
            List<ReturnsPo> returnsPoList = orderService.getReturnsId(orderReturnsVos.getOrderId());
            for (int i = 0; i < returnsPoList.size(); i++) {
                ReturnsPo returnsPo = returnsPoList.get(i);
                orderService.addReturnsDetails(returnsPo.getOrderReturnsId(),orderReturnsVos.getGoodsType(),orderReturnsVos.getGoodsName(),
                        orderReturnsVos.getGoodsPrice(),orderReturnsVos.getGoodsCount(),1);
            }

            int i = orderService.updateStatus(1, orderReturnsVos.getGoodsName());
        }
        System.out.println("resultVO = " + resultVO);
        return resultVO;
    }
    @GetMapping("returnsDetail")
    public ResultVO returnsDetail(String orderId){
        System.out.println(orderId);
        List<ReturnsDetailsDto> returnsDetailsVo = orderService.returnsDetails(orderId);
        ResultVO resultVO = null;
        if(returnsDetailsVo.size()!=0){
            resultVO = ResultVO.success(returnsDetailsVo);
            rabbitTemplate.convertAndSend("frontExchangetest","returnsDetail",returnsDetailsVo);
            for (int i = 0; i <returnsDetailsVo.size() ; i++) {
                ReturnsDetailsDto returnsDetailsDto = returnsDetailsVo.get(i);
                orderService.updateStatus(2,returnsDetailsDto.getGoodsName());
            }
        }
        else {
            resultVO = ResultVO.fail(ResultCode.GET_RETURNS_FAILED);
        }
        return resultVO;
    }
    @GetMapping("setRedis")
    public Boolean setRedis(){
        List<CartsVo> list = new ArrayList<>();
        CartsVo cartsVo1 = new CartsVo();
        cartsVo1.setGoodsName("华为Mate40");
        cartsVo1.setPrice(4999.0);
        cartsVo1.setNum(1);
        cartsVo1.setGoodsInfo("震撼大屏 观影更爽 / 时尚全面屏设计 视野更广阔 ");
        cartsVo1.setGoodsType("电子产品");
        cartsVo1.setGoodPicture("HuaweiMate40.png");
        CartsVo cartsVo2 = new CartsVo();
        cartsVo2.setGoodsName("华为P40");
        cartsVo2.setPrice(5988.0);
        cartsVo2.setNum(2);
        cartsVo2.setGoodsInfo("震撼大屏 观影更爽 / 时尚全面屏设计 视野更广阔 ");
        cartsVo2.setGoodsType("电子产品");
        cartsVo2.setGoodPicture("HuaweiP40.png");
        CartsVo cartsVo3 = new CartsVo();
        cartsVo3.setGoodsName("苹果12");
        cartsVo3.setPrice(6799.0);
        cartsVo3.setNum(1);
        cartsVo3.setGoodsInfo("震撼大屏 观影更爽 / 时尚全面屏设计 视野更广阔 ");
        cartsVo3.setGoodsType("电子产品");
        cartsVo3.setGoodPicture("iPhone12.png");
        list.add(cartsVo1);
        list.add(cartsVo2);
        list.add(cartsVo3);
        boolean b = redisCacheUtil.set("yyqx", list);
        return b;
    }
    @GetMapping("popRedis/{userName}")
    public ResultVO popRedis(@PathVariable("userName") String userName){
        System.out.println("userName = " + userName);
        Object o = redisCacheUtil.get(userName);
        System.out.println(o);
        ResultVO resultVO = null;
        resultVO = ResultVO.success(o);
        return resultVO;
    }
}
